$(function () {
    $(document).ready(function () {
        var userId = location.search.substring(1);
        var userInfo = {"userid": userId};
        var fields = new Array('tcname', 'qbye', 'cs','endtime');
        var url = "/user/showqbye";
        $.ajax(
            {
                type: "post",
                url: url,
                data: userId,
                sync:false,
                dataType: "json",
                success: function (data) {
                    var result = data.data;
                    var str = "";
                    for (var i = 0; i < result.length; i++) {
                        var endtime=result[i].endtime;
                        var now = new Date;
                        var d = new Date(endtime);
                        if (now > d) {

                            str += "<tr style='color:red'>";
                        }else{
                            str += "<tr>";
                        }

                        for (x in fields) {
                            for (var key in result[i]) {
                                if (fields[x] == key) {
                                    if(key=="qbye"){
                                        str += "<td>" + back_money_handler(result[i][key]/100) + "</td>";

                                    }else{
                                        str += "<td>" + result[i][key] + "</td>";

                                    }

                                }
                            }
                        }
                    }
                    $("#qbbody").html(str);
                }, error: function () {
                alert("报错了");
            }
            }
        );


        //M站消费记录

        $.ajax({
            type: "post",
            url: "/user/mpayflow",
            data: userId,
            sync:false,
            dataType: "json",
            success:function(data){
                var result=data.data;
                var tmphtml=bt('mbodyscript', result);
                $("#mbody").html(tmphtml);
            }


        });
        //M站消费记录

        $.ajax({
            type: "post",
            url: "/user/wxbody",
            data: userId,
            sync:false,
            dataType: "json",
            success:function(data){
                var result=data.data;
                var tmphtml=bt('wxbodyscript', result);
                $("#wxbody").html(tmphtml);
            }


        });

        //M站充值记录

        $.ajax({
            type: "post",
            url: "/user/paymz",
            data: userId,
            sync:false,
            dataType: "json",
            success:function(data){
                var result=data.data;
                var tmphtml=bt('paymzbodyscript', result);
                $("#paymzbody").html(tmphtml);
            }

        });

        //公众号充值记录

        $.ajax({
            type: "post",
            url: "/user/paywx",
            data: userId,
            sync:false,
            dataType: "json",
            success:function(data){
                var result=data.data;
                var tmphtml=bt('paywxbodyscript', result);
                $("#paywxbody").html(tmphtml);
            }


        });




    });

    $('.edit-remark').on('click', function () {
        $('#remark').cmsmodal('show');
        $('#note').val($('#remark_div')["0"].innerText);
    })
    $('.remark-confirm').on('click', function () {
        //确定
        var note = $('#note')[0].value;
        var userInfo = {
            remark: note,
            userid: getQueryString('uid')
        }
        $.ajax(
            {
                type: "post",
                url: "/user/updatereMark",
                data: userInfo,
                dataType: "json",
                success: function (data) {
                    $('.remark').html(note);
                }, error: function () {
                alert("报错了");
            }
            }
        );
        $('#remark').cmsmodal('hide');
    })
    $('.remark-cancel').on('click', function () {
        //取消
        $('#remark').cmsmodal('hide');
    });


    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    $('.account-freeze').on('click',function(){
        var btntext = ''
        if($(this).html()=='账户冻结'){
            btntext = '账户解冻';
            $('.freeze-quesition').html('确认冻结该客户账户吗？' );
            $('.freeze-info').html('冻结后该客户将不能实现咨询、关注律师、更换专属律师、充值等行为。');
        }else if($(this).html()=='账户解冻'){
            btntext = '账户冻结';
            $('.freeze-quesition').html('确认将客户账户恢复正常吗？' );
            $('.freeze-info').html('解冻后该客户账户功能将恢复正常。');
        }
        $('#freeze-info').show();
        $('.freeze-confirm').off('click').on('click',function(){
            //确定

            //客户冻结
            var arrayObj = new Array();
            arrayObj.push(getQueryString('uid'));
            var  json={list:arrayObj};
            var url;
            var btn=$('.account-freeze').html();
            if(btn=='账户解冻'){
                url="/user/userunfrozeAccount";
            }else{
                url="/user/userfrozeAccount";
            }
            $.ajax(
                {
                    type: "post",
                    url: url,
                    data: json,
                    dataType: "json",
                    success: function (data) {
                        if(btntext){
                            $('.account-freeze').html(btntext);
                            if(btntext=='账户解冻'){
                                $("#ustates").html("冻结");
                            }else{
                                $("#ustates").html("正常");
                            }

                        }
                    }, error: function () {
                    alert("报错了");
                }
                }
            );

            $('#freeze-info').hide();
        })
        $('.freeze-cancel').off('click').on('click',function(){
            //取消
            btntext = '';
            $('#freeze-info').hide();
        })
    })

})